package backend;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.ole.win32.Variant;
import org.eclipse.swt.events.*;

import frontend.WMPlayer;

public class randTest {

	protected Shell shell;
	private Text text;
	private WMPlayer player;
	private Composite composite_1;
	private Button btnNewButton;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			randTest window = new randTest();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(450, 300);
		shell.setText("SWT Application");
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		player = new WMPlayer(composite, SWT.NONE, SWT.NONE);
		
		composite_1 = new Composite(shell, SWT.NONE);
		composite_1.setLayout(new FormLayout());
		
		text = new Text(composite_1, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL);
		FormData fd_text = new FormData();
		fd_text.top = new FormAttachment(0);
		fd_text.left = new FormAttachment(0);
		text.setLayoutData(fd_text);
		
		btnNewButton = new Button(composite_1, SWT.NONE);
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				runCommand(text.getText());
			}
		});
		fd_text.right = new FormAttachment(btnNewButton, 0, SWT.RIGHT);
		fd_text.bottom = new FormAttachment(btnNewButton, -6);
		FormData fd_btnNewButton = new FormData();
		fd_btnNewButton.left = new FormAttachment(0);
		fd_btnNewButton.right = new FormAttachment(100);
		fd_btnNewButton.bottom = new FormAttachment(0, 262);
		fd_btnNewButton.top = new FormAttachment(0, 231);
		btnNewButton.setLayoutData(fd_btnNewButton);
		btnNewButton.setText("Run Command");

	}
	
	private void runCommand(String cmnd){
		try{
			if (cmnd.contains("get>")){
				String propName = cmnd.split(">")[1];
				this.text.setText(this.player.getProperty(propName));
			}else if (cmnd.indexOf("set>")==0){
				String propName = cmnd.split(">")[1];
				String propVal = cmnd.split(">")[2];
				if (propName.compareTo("currentPosition") == 0){
					Variant var = new Variant(Double.parseDouble(propVal));
					this.player.setProperty(propName, var);
				}
			}else if (cmnd.indexOf("proplist>")==0){
				Integer maxID = Integer.parseInt(cmnd.split(">")[1]);
				this.text.setText(this.player.listControlPropertyNames(maxID));
			}else if (cmnd.indexOf("open>")==0){
				String path = cmnd.split(">")[1];
				this.player.setVideo(path);
			}else if (cmnd.indexOf("sitelist>")==0){
				Integer maxID = Integer.parseInt(cmnd.split(">")[1]);
				this.text.setText(this.player.listSitePropertyNames(maxID));
			}else if (cmnd.indexOf("goto>")==0){
				Double time = Double.parseDouble(cmnd.split(">")[1]);
				this.player.goTo(time);
			}
		}catch(NullPointerException e){
		}catch(NumberFormatException e){
		}catch(IndexOutOfBoundsException e){
		}
	}
}
